package com.bw.ivideoproject.home

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.ActivitySearchListBinding
import com.bw.ivideoproject.entity.SimpleVideoEntity
import com.bw.lib_mvvm.BaseActivity

class SearchListActivity : BaseActivity<ActivitySearchListBinding>() {

    private var keyword:String = ""
    private val viewModel:HomeViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }
    private lateinit var videoAdapter: SimpleVideoAdapter

    override fun initView() {
        keyword = intent.getStringExtra("keyword") as String
        videoAdapter = SimpleVideoAdapter(this)
        binding.rvSearch.apply {
            this.adapter = videoAdapter
            this.layoutManager = LinearLayoutManager(this@SearchListActivity)
        }
        binding.tvNormalTitleBar.text = keyword
    }

    override fun initData() {
        viewModel.getVideoByKeyword(keyword,0,10)

        binding.back.setOnClickListener{
            finish()
        }

        videoAdapter.setOnItemClickListener(object : SimpleVideoAdapter.OnItemClickListener{
            override fun OnItemClick(item: SimpleVideoEntity) {
                var intent = Intent(this@SearchListActivity,DetailActivity::class.java)
                intent.putExtra("video",item)
                startActivity(intent)
            }
        })
    }

    override fun handleObserver() {
        super.handleObserver()
        viewModel.keywordLiveData.observe(this){
            videoAdapter.refresh(it,true)
        }
    }

    override fun bindLayout(): Int {
        return R.layout.activity_search_list
    }

}